package com.songhq.desktop.controller;

import com.songhq.desktop.fxml.IndexView;
import com.songhq.desktop.model.ToolFxmlLoaderConfiguration;
import com.songhq.desktop.utils.Config;
import de.felixroske.jfxsupport.FXMLController;
import javafx.scene.control.Menu;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.extern.slf4j.Slf4j;
import com.songhq.desktop.utils.DesktopToolSystemUtil;
import org.apache.commons.lang.StringUtils;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
@FXMLController
@Slf4j
public class IndexController extends IndexView {
    private Map<String, Menu> menuMap = new HashMap<String, Menu>();

    public IndexController() throws Exception {

    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //初始化
      log.info("----------------初始化ing-----------------");
      this.bundle = resources;



    }

    private void initView(){
        List<ToolFxmlLoaderConfiguration> toolList = DesktopToolSystemUtil.loaderToolFxmlLoaderConfiguration();
        menuMap.put("toolsMenu", toolsMenu);

        for (ToolFxmlLoaderConfiguration toolConfig : toolList) {
            try {
                if (StringUtils.isEmpty(toolConfig.getResourceBundleName())) {
                    if (StringUtils.isNotEmpty(bundle.getString(toolConfig.getTitle()))) {
                        toolConfig.setTitle(bundle.getString(toolConfig.getTitle()));
                    }
                } else {
                    ResourceBundle resourceBundle = ResourceBundle.getBundle(toolConfig.getResourceBundleName(),
                            Config.defaultLocale);
                    if (StringUtils.isNotEmpty(resourceBundle.getString(toolConfig.getTitle()))) {
                        toolConfig.setTitle(resourceBundle.getString(toolConfig.getTitle()));
                    }
                }
            } catch (Exception e) {
                log.error("加载菜单失败", e);
            }
            if (toolConfig.getIsMenu()) {
                Menu menu = new Menu(toolConfig.getTitle());
                if (StringUtils.isNotEmpty(toolConfig.getIconPath())) {
                    ImageView imageView = new ImageView(new Image(toolConfig.getIconPath()));
                    imageView.setFitHeight(18);
                    imageView.setFitWidth(18);
                    menu.setGraphic(imageView);
                }
                menuMap.put(toolConfig.getMenuId(), menu);
            }
        }

        for (ToolFxmlLoaderConfiguration toolConfig : toolList) {
            if (toolConfig.getIsMenu()) {
                menuMap.get(toolConfig.getMenuParentId()).getItems().add(menuMap.get(toolConfig.getMenuId()));
            }
        }
    }
}
